package modules

import (
	"awesomeProject/crawler/engine"
)

var WorkerCount int
var requests []engine.Request
var ItemHandler func([]interface{})

func Init(workerCount int, itemHandler func([]interface{}), seeds ...engine.Request) {
	WorkerCount = workerCount
	ItemHandler = itemHandler
	for _, r := range seeds {
		requests = append(requests, r)
	}
}

func Run() {
	var publicIn = make(chan engine.Request)
	var publicOut = make(chan engine.WorkResult)
	for i := 0; i < WorkerCount; i++ {
		createWorker(publicIn, publicOut)
	}
	for {
		result := <-publicOut
		addRequest(result.Requests...)
		ItemHandler(result.Items)
		for {
			if req, ok := PopRequest(); ok {
				//防止in与out相互等待
				go func() {
					publicIn <- req
				}()
			} else {
				break
			}
		}
	}
}

func createWorker(publicIn chan engine.Request, publicOut chan engine.WorkResult) {
	go func() {
		for {
			req := <-publicIn
			publicOut <- req.Worker(req.Url)
		}
	}()
}

// To improve
func addRequest(req ...engine.Request) {
	requests = append(requests, req...)
}

func PopRequest() (engine.Request, bool) {
	var r engine.Request
	if len(requests) > 0 {
		r = requests[0]
		requests = requests[1:]
	} else {
		return r, false
	}
	return r, true
}
